<!DOCTYPE html>
<html>
<head>
	<title>File browser</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="Cache-Control" content="No-Cache" />
	<meta http-equiv="Pragma" content="No-Cache" />
	<meta http-equiv="Expires" content="-1" />

	<link type="image/ico" rel="shortcut icon" href="<?php echo PUBLIC_URL; ?>images/favicon.ico" />
	<link type="text/css" rel="stylesheet" href="<?php echo PUBLIC_URL; ?>ext/bootstrap/css/bootstrap.min.css" />
	<link type="text/css" rel="stylesheet" href="<?php echo PUBLIC_URL; ?>styles/styles.css" />
	<link type="text/css" rel="stylesheet" href="<?php echo PUBLIC_URL; ?>styles/schema.css" />
<?php if (Config::get('rtl', 'i18n')) { ?>
	<link type="text/css" rel="stylesheet" href="<?php echo PUBLIC_URL; ?>styles/rtl.css" />
<?php } ?>

	<script type="text/javascript" src="<?php echo PUBLIC_URL; ?>scripts/scripts.js"></script>
	<script type="text/javascript" src="<?php echo PUBLIC_URL; ?>ext/jquery/jquery-1.8.1.min.js"></script>
	<script type="text/javascript" src="<?php echo PUBLIC_URL; ?>ext/bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="<?php echo PUBLIC_URL; ?>ext/tiny_mce/jquery.tinymce.js"></script>
	<script type="text/javascript" src="<?php echo PUBLIC_URL; ?>ext/tiny_mce/tiny_mce_popup.js"></script>

	<script type="text/javascript">
	Registry.base_url = '<?php echo BASE_URL; ?>';
	Registry.public_url = '<?php echo PUBLIC_URL; ?>';
	Registry.upload_url = '<?php echo UPLOAD_URL; ?>';
	Registry.current_path = '<?php echo Router::getCurrentPath(); ?>';
	Registry.language_rtl = <?php echo (Config::get('rtl', 'i18n') ? 'true' : 'false'); ?>;

	var fileURL = "";
	var FileBrowserDialogue = {
	    init : function () {
	        // Here goes your code for setting your custom things onLoad.
	    },
	    sendImageURL : function () {
	        // Here goes your code to insert the retrieved URL value into the original dialogue window.
	        // For example code see below.
	        var win = tinyMCEPopup.getWindowArg("window");

	        // insert information now
	        win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = fileURL;

			// for image browsers: update image dimensions
			if (typeof(win.ImageDialog) != "undefined") {
				if (win.ImageDialog.getImageData) win.ImageDialog.getImageData();
				if (win.ImageDialog.showPreviewImage) win.ImageDialog.showPreviewImage(fileURL);
			}
			
			// set preview for media browser
			if (typeof(win.Media) != "undefined") {
				win.Media.formToData('type');
			}
			
	        // close popup window
	        tinyMCEPopup.close();
	    }
	}

	tinyMCEPopup.onInit.add(FileBrowserDialogue.init, FileBrowserDialogue);
	
	function setFile(url) {
		fileURL = url;
		FileBrowserDialogue.sendImageURL();
	}
	</script>
</head>
<body style="background:#fff; padding:5px;">

<table border="0" bordercolor="red" width="100%" height="100%" cellspacing="0" cellpadding="0">
	<tr>
		<td style="width:250px; padding-right:10px;">
			<?php
			foreach ($cat_list as $category) {
				$class_name = 'simple_tree_off';
				if ($cat_id == intval($category['id'])) {
					$class_name = 'simple_tree_on';
				} ?>
				<a href="<?php echo CURRENT_URL, $category['id']; ?>" class="<?php echo $class_name; ?>" style="margin-left:<?php echo ($category['level'] * 5); ?>px;">
					<div class="simple_tree_item_off" cat_id="<?php echo $category['id']; ?>">
						<?php echo $category['name']; ?>
					</div>
				</a>
			<?php } ?>
		</td>
		<td style="padding:0px 0px 0px 5px;">
		<?php if (count($file_list) > 0) {
			$max_length = 100;
			foreach ($file_list as $file) {
				$style = '';
				if (in_array($file['extension'], Config::get('file_images_extensions'))) {
					if (!is_null($file['name_thumb'])) {
						$preview = UPLOAD_URL . $file['name_thumb'];
					} else {
						$preview = UPLOAD_URL . $file['name'];
					}

					if ((intval($file['width']) > $max_length) || (intval($file['height']) > $max_length)) {
						if (intval($file['width']) >= intval($file['height'])) {
							$style .= 'width:' . $max_length . 'px;';
						} else {
							$style .= 'height:' . $max_length . 'px;';
						}
					}
				} else {
					$file_icon = File::getIcon($file['extension']);
					$preview = PUBLIC_URL . 'images/file/' . $file_icon;
					$style = 'width:48px; height:48px;';
				}
				$popup = $file['name_orig']; ?>
				<div style="padding:0px 5px 5px 0px; float:left;">
					<div title="<?php echo $popup; ?>" class="files_item" file_id="<?php echo $file['id']; ?>" style="width:112px; height:140px; border:1px solid #ccc; background:#eee; padding:5px; cursor:pointer;" onclick="setFile('<?php echo UPLOAD_URL . $file['name']; ?>')">
						<div style="width:110px; height:110px; display:table-cell; vertical-align:middle; border:1px solid #ccc; background:#fff;"><img src="<?php echo $preview; ?>" style="display:block; margin:auto; <?php echo $style; ?>" alt="File" /></div>
						<div style="font-size:0.8em; padding-top:5px;">
							<div><b>Size:</b> <?php echo File::formatSize($file['size']); ?></div>
							<?php if ((intval($file['width']) > 0) && (intval($file['height']) > 0)) {?>
								<div><b>Dim:</b> <?php echo $file['width'], ' x ', $file['height']; ?></div>
							<?php } ?>
						</div>
					</div>
				</div>
			<?php }
		} else { ?>
			No files found
		<?php } ?>
		</td>
	</tr>
</table>

</body>
</html>